package com.zhongyu.AreaJson.service.Impl;

import com.zhongyu.AreaJson.mapper.VAreaMapper;
import com.zhongyu.AreaJson.model.VArea;
import com.zhongyu.AreaJson.service.AreaService;
import com.zhongyu.AreaJson.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.List;

/**
 * @Auther: zhongyu
 * @Date: 2018/8/24 16:11
 * @Description:echarts全国JSON数据包-包含全国省市县区资源，标准地理数据 GEOJSON
 */
@Service
public class AreaServiceImpl implements AreaService{
    @Autowired
    private VAreaMapper vAreaMapper;
    /**
     * 根据区划编码获取区域Json数据
     *
     * @param AreaCode
     * @return
     * @throws Exception
     */
    @Override
    public String getJsonByAreaCode(String AreaCode) throws Exception {
        File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "area/" + AreaCode + ".json");
        if(file.exists()){
            String json = FileUtil.read(file.getPath());
            return json;
        }else {
            return "区划不存在";
        }
    }

    /**
     * @param id
     * @author: zhongyu
     * @description:
     * @param: [id]
     * @return: com.zhongyu.AreaJson.model.VArea
     * @date: 2018/9/10 17:37
     */
    @Override
    public VArea getAreaById(String id) {
        VArea vArea = vAreaMapper.selectByPrimaryKey(Integer.parseInt(id));
        return vArea;
    }

    /**
     * @param areaCode
     * @author: zhongyu
     * @description:
     * @param: [areaCode]
     * @return: com.zhongyu.AreaJson.model.VArea
     * @date: 2018/9/11 8:21
     */
    @Override
    public VArea getAreaByAreaCode(String areaCode) {
        VArea vArea = null;
        if (areaCode != null && !"".equals(areaCode)) {
            vArea = vAreaMapper.getByAreaCode(Integer.parseInt(areaCode));
        }
        return vArea;
    }

    /**
     * @param vArea
     * @author: zhongyu
     * @description:
     * @param: [vArea]
     * @return: java.lang.Boolean
     * @date: 2018/9/11 8:30
     */
    @Override
    public Boolean updateAarea(VArea vArea) {
        return vAreaMapper.updateByPrimaryKeyWithBLOBs(vArea)>0;
    }
}
